/*
 * @Author: Leo.wang wanglizhigs@163.com
 * @Date: 2025-07-02 21:37:05
 * @LastEditors: Leo.wang wanglizhigs@163.com
 * @LastEditTime: 2025-07-03 22:32:47
 * @FilePath: /book-manage-angular/src/app/pages/login/login.component.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https
 */
import { Component, OnInit } from '@angular/core';
import { ReactiveFormsModule, FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { AuthService } from '../../services/auth.service';

import { CommonModule } from '@angular/common';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzIconModule } from 'ng-zorro-antd/icon';

import { NzAlertModule } from 'ng-zorro-antd/alert';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less'],
  imports: [CommonModule,
    ReactiveFormsModule,
    NzFormModule,
    NzInputModule,
    NzButtonModule,
    NzIconModule, NzAlertModule],
})
export class LoginComponent implements OnInit {
  loading = false;
  loginForm!: FormGroup;
  passwordVisible = false;

  constructor(
    private fb: FormBuilder,
    private authService: AuthService,
    private router: Router,
    private nzMessage: NzMessageService
  ) { }

  ngOnInit(): void {

    this.loginForm = this.fb.group({
      username: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(20)]],
      password: ['', [Validators.required, Validators.minLength(6), Validators.maxLength(20)]]
    });


    if (this.authService.isAuthenticated()) {
      this.router.navigate(['/']);
    }
  }


  handleLogin(): void {

    this.loginForm.markAllAsTouched();

    if (this.loginForm.valid) {
      this.loading = true;
      const { username, password } = this.loginForm.value;


      setTimeout(() => {
        const result = this.authService.validateCredentials(username, password);

        if (result.success) {
          this.authService.setAuthToken(result.user || null);
          this.nzMessage.success('登录成功！');
          this.router.navigate(['/']);
        } else {
          this.nzMessage.error(result.message || '登录失败！');
        }
        this.loading = false;
      }, 1000);
    }
  }


  get username() {
    return this.loginForm.get('username');
  }

  get password() {
    return this.loginForm.get('password');
  }


  get usernameErrorTip(): string {
    if (this.username?.errors?.['required']) {
      return '请输入用户名';
    }
    if (this.username?.errors?.['minlength'] || this.username?.errors?.['maxlength']) {
      return '用户名长度在 2 到 20 个字符';
    }
    return '';
  }


  get passwordErrorTip(): string {
    if (this.password?.errors?.['required']) {
      return '请输入密码';
    }
    if (this.password?.errors?.['minlength'] || this.password?.errors?.['maxlength']) {
      return '密码长度在 6 到 20 个字符';
    }
    return '';
  }


  togglePasswordVisibility(): void {
    this.passwordVisible = !this.passwordVisible;
  }
}
